#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time     : 2018/2/22 14:06
# @Author   : BYF
# file      : login_page.py
# @version  :

from base_page import BasePage
from selenium.webdriver.common.by import By
from common import function


class LoginPage(BasePage):

    #Action
    username_loc = (By.CSS_SELECTOR, "div>input[name='name']")  # 用户名输入框
    password_loc = (By.CSS_SELECTOR, "div>input[name='password']")  # 密码输入框
    loginbutton_loc = (By.CSS_SELECTOR, "div>button[type='submit']")  # 登录按钮
    login_error_loc = (By.CSS_SELECTOR, ".login-error" and '[style="display: block;"]') # 定位错误提示信息

    def input_username(self, username):
        self.send_keys(self.username_loc,username)

    def input_password(self, password):
        self.send_keys(self.password_loc, password)

    def click_button(self):
        self.driver.find_element(*self.loginbutton_loc).click()

    def _open(self):
        base_url = self.config.get("url", "base_url")
        login_url = self.config.get("url", "login_url")
        self.open(base_url + login_url)

    # 错误提示
    def login_error(self):
        return self.driver.find_element(*self.login_error_loc).text

    # 登录
    def login_in(self, username, password):
        self._open()
        self.input_username(username)  # 输入用户名
        self.input_password(password)  # 输入密码
        self.click_button()  # 点击登录按钮